package com.lzkj.baotouhousingfund.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.coderc.library.view.statusbar.StatusBarUtil;
import com.lzkj.baotouhousingfund.R;
import com.lzkj.baotouhousingfund.app.App;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by Chengs on 2017/4/6.
 * 无MVP的基类
 */


public abstract class SimpleActivity extends RxAppCompatActivity {

    protected Activity           mContext;
    private   Unbinder           mUnBinder;
    private   InputMethodManager mInputMethodManager;

    protected CompositeDisposable mCompositeDisposable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        initStatusBarDefColor();
        mUnBinder = ButterKnife.bind(this);
        mContext = this;
        App.getInstance().addActivity(this);
        initEventAndData();
        //如果要使用eventbus请将此方法返回true
        if (useEventBus()) {
            //注册到事件主线
            EventBus.getDefault().register(this);
        }
    }

    protected void initStatusBarDefColor() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.colorTheme), 5);
    }


    protected void unDisposable() {
        if (null != mCompositeDisposable) {
            mCompositeDisposable.clear();
        }
    }

    protected void addDisposable(Disposable disposable) {
        if (null == mCompositeDisposable) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(disposable);
    }

    protected ProgressDialog showSpinnerDialog() {
        //activity = modifyDialogContext(activity);
        ProgressDialog dialog = new ProgressDialog(this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setCancelable(true);
        dialog.setMessage(getString(R.string.progress_loading));
        if (!isFinishing()) {
            dialog.show();
        }
        return dialog;
    }

    protected void setToolBar(Toolbar toolbar, String title) {
        toolbar.setTitle(title);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }

    public void hideSoftInput(View view) {
        mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mInputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    protected void alwaysShowMenuItem(Menu menu) {
        if (null != menu && menu.size() > 0) {
            MenuItem item = menu.getItem(0);
            item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
                    | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (useEventBus())//如果要使用eventbus请将此方法返回true
            EventBus.getDefault().unregister(this);
        App.getInstance().removeActivity(this);
        mUnBinder.unbind();
        unDisposable();
    }

    public boolean useEventBus() {
        return false;
    }


    protected abstract int getLayout();

    protected abstract void initEventAndData();
}

